﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.Common.Security;
using ScrumTable.DL.Data.TFS.Properties;

#endregion


namespace ScrumTable.DL.Data.TFS.Services.TeamFoundation
{
    /// <summary>
    /// This class is used in order to connect to the Microsoft
    /// Team Foundation server. The connection will be established
    /// in an synchronous manner but can be also used in an asynchronous
    /// way.
    /// </summary>
    public class TfsConnector
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly ConnectionInformation _toConnect;
        private readonly Uri _backEndSystemUri;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new TfsConnector instance.
        /// </summary>
        public TfsConnector(ConnectionInformation toConnect, Uri backEndSystemUri)
        {
            PreCondition.AssertNotNull(toConnect, "toConnect");
            PreCondition.AssertNotNull(backEndSystemUri, "backEndSystemUri");

            _toConnect = toConnect;
            _backEndSystemUri = backEndSystemUri;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Connects to the MS team foundation server and retrieves data.
        /// </summary>
        public TfsServerWrapper Connect()
        {
            TfsServerWrapper serverWrapper = DoConnect();

            if (serverWrapper == null)
                throw new TfsConnectorException(Resources.ExcTfsConnectionException);

            return serverWrapper;
        }

        private TfsServerWrapper DoConnect()
        {
            TfsServerWrapper tfs = null;

            try
            {
                tfs = EstablishConnection();

                if (tfs != null && !tfs.HasAuthenticated)
                    tfs = null;
            }
            catch (Exception)
            {
                if (tfs != null)
                {
                    tfs.Dispose();
                    tfs = null;
                }
                throw;
            }
            return tfs;
        }

        private TfsServerWrapper EstablishConnection()
        {
            TfsServerWrapper tfs = new TfsServerWrapper(_toConnect, _backEndSystemUri);
            tfs.EnsureAuthenticated();
            return tfs;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}